package cn.yunhe.controller;

import cn.yunhe.entity.Distance;
import cn.yunhe.service.IDistanceService;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;


@WebServlet("/before/DistanceServlet")
public class DistanceServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/json;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String po1 = request.getParameter("chufa");
        String po2 = request.getParameter("daoda");

        IDistanceService iDistanceService = new IDistanceService();

        double gongli = iDistanceService.getDistanceTo(po1,po2);

        DecimalFormat df = new DecimalFormat("#.00");
        String _gongli =df.format(gongli);
        System.out.println(gongli);
        JSONObject j = new JSONObject();
        j.put("gongli",_gongli);
        out.print(j);

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
